package vn.orderonline.core.dao.toorder;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import vn.orderonline.core.commons.utils.StringUtils;
import vn.orderonline.core.dao.common.GenericDaoImpl;
import vn.orderonline.core.dao.entity.ToOrder;

@Repository
public class ToOrderDaoImpl extends GenericDaoImpl<ToOrder, Integer> implements ToOrderDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> getAllOrderBill() {
		String hql = "Select too.orderCode, too.username, too.email, too.wardName, "
				+ "too.phoneNumber, re.restaurantName, bi.totalPrice, too.createTime "
				+ "From ToOrder too, Bill bi, Restaurant re "
				+ "Where too.orderId = bi.orderId And too.restaurantId = re.restaurantId "
				+ "Order By too.createTime DESC";
		Query query = getSession().createQuery(hql);
		return query.list();
	}

	@Override
	public Integer getOrderIdByOrderCode(String orderCode) {
		Criteria criteria = getSession().createCriteria(ToOrder.class);
		criteria.setProjection(Projections.property("orderId"));
		criteria.add(Restrictions.eq("orderCode", orderCode));
		return (Integer) criteria.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> search(String orderCode, String username,
			String email, String wardName, String phoneNumber,
			String restaurantName) {
		String hql = "Select too.orderCode, too.username, too.email, too.wardName, "
				+ "too.phoneNumber, re.restaurantName, bi.totalPrice, too.createTime "
				+ "From ToOrder too, Bill bi, Restaurant re "
				+ "Where too.orderId = bi.orderId And too.restaurantId = re.restaurantId ";
		StringBuilder builder = new StringBuilder(hql);
		if (!StringUtils.isNullOrEmpty(orderCode)) {
			builder.append("And too.orderCode = :orderCode ");
		}
		if (!StringUtils.isNullOrEmpty(username)) {
			builder.append("And too.username = :username ");
		}
		if (!StringUtils.isNullOrEmpty(email)) {
			builder.append("And too.email = :email ");
		}
		if (!StringUtils.isNullOrEmpty(wardName)) {
			builder.append("And too.wardName like :wardName ");
		}
		if (!StringUtils.isNullOrEmpty(phoneNumber)) {
			builder.append("And too.phoneNumber like :phoneNumber ");
		}
		if (!StringUtils.isNullOrEmpty(restaurantName)) {
			builder.append("And re.restaurantName like :restaurantName ");
		}
		builder.append("Order By too.createTime DESC ");
		Query query = getSession().createQuery(builder.toString());
		if (!StringUtils.isNullOrEmpty(orderCode)) {
			query.setParameter("orderCode", orderCode);
		}
		if (!StringUtils.isNullOrEmpty(username)) {
			query.setParameter("username", username);
		}
		if (!StringUtils.isNullOrEmpty(email)) {
			query.setParameter("email", email);
		}
		if (!StringUtils.isNullOrEmpty(wardName)) {
			query.setParameter("wardName", "%" + wardName + "%");
		}
		if (!StringUtils.isNullOrEmpty(phoneNumber)) {
			query.setParameter("phoneNumber", "%" + phoneNumber + "%");
		}
		if (!StringUtils.isNullOrEmpty(restaurantName)) {
			query.setParameter("restaurantName", "%" + restaurantName + "%");
		}
 		return query.list();
	}

}
